<?php

namespace App\Http\Controllers\App;

use App\Http\Controllers\Controller;
use App\Http\Resources\App\UserProfileResource;
use Illuminate\Http\Request;

class UserController extends Controller
{
    public function profile(Request $request)
    {
//        dd($request->user()->learnCourseCount());
        $data = $request->user()->load(['careers' => function ($query) {
            return $query->wherePivot('is_selected', 1);
        }, 'courses' => function ($query) {
            return $query->withPivot('completed_at')->orderBy('pivot_completed_at', 'desc')->limit(1);
        }, 'profile'])->loadCount('courses');
//        dd($data->toArray());
        if ($data->courses and $data->careers and $data->profile) {
            return new UserProfileResource($data);
        } else {
            abort('400', '您还未在系统进行学习');
        }

    }
}
